home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 3⁄23⁄90 / 0083-THzBlock class-Mar90 next >
Encoding:
Text File  |  1990-03-23  |  1.6 KB  |  59 lines  |  [TEXT/GEOL]

  1. Item    7256363                         19-March-90        10:23PST
  2.  
  3. From:   NAUTIL                          France - Dev, Nautil Info Lyon,IDV
  4.  
  5. To:     CPLUS.APPLE$                    C++ Interest List--Apple Employees
  6.         CPLUS.DEV$                      C++ Interest List--Developers
  7.  
  8. Sub:    THzBlock class
  9.  
  10. Hi,
  11. Do you remember my problems with allocation ambiguity?!
  12.  
  13. Well, reading "C++ Reference manual" again and again, I discovered some
  14. solution: the 'placement' syntax can supply additional arguments to operator
  15. new. So I now define a base class wich can to be allocated with any Memory
  16. Manager call :
  17.  
  18. #include <StdDef.h>
  19. #include <Memory.h>
  20. // If using MacApp permanent allocation :
  21. #include <UMemory.h>
  22.  
  23. typedef pascal Ptr (*new_p)(Size);
  24.  
  25. class THzBlock {
  26. public:
  27.    void* operator new(size_t size, new_p mmgrNew) { return (*mmgrNew)(size); }
  28.    void operator delete(void* p);
  29. };
  30.  
  31.  
  32. void THzBlock::operator delete(void* p)
  33. // Current zone is set to the zone where the block is located.
  34. // (see IM II-26 warning).
  35. // ??? But, is it really necessary ???
  36. {
  37.    THz oldZone = GetZone();
  38.    SetZone(PtrZone((Ptr)p));
  39.    DisposPtr((Ptr)p);
  40.    SetZone(oldZone);
  41. }
  42.  
  43.  
  44. void sample(void)
  45. {
  46.    THzBlock* currentHeapBlock = new (NewPtr) THzBlock;
  47.    THzBlock* systemHeapBlock = new (NewPtrSys) THzBlock;
  48.    THzBlock* clearBlock = new (NewPtrClear) THzBlock;
  49.    THzBlock* sysClearBlock = new (NewPtrSysClear) THzBlock;
  50.    THzBlock* permMacAppBlock = new (NewPermPtr) THzBlock;
  51. }
  52.  
  53.  
  54. I hope this class can help every one interested in using direct Memory Manager
  55. calls to allocate C++ objects.
  56.  
  57. Etienne Vautherin
  58.  
  59.